<!DOCTYPE HTML>
<html  lang="zh" xmlns:th="http://www.thymeleaf.org">
<meta charset="utf-8">
<head th:include="include :: header"></head>
<link th:href="@{/ajax/libs/summernote/summernote.css}" rel="stylesheet"/>
<link th:href="@{/ajax/libs/summernote/summernote-bs3.css}" rel="stylesheet"/>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-zhengqu-edit" th:object="${zhengqu}">
            <input id="zqId" name="zqId" th:field="*{zqId}"  type="hidden">
			<div class="form-group">	
				<label class="col-sm-3 control-label">标题：</label>
				<div class="col-sm-8">
					<input id="zqTitle" name="zqTitle" th:field="*{zqTitle}" class="form-control" type="text">
				</div>
			</div>
			<div class="form-group">	
				<label class="col-sm-3 control-label">变更日期：</label>
				<div class="col-sm-8">
					<input id="zqNewChangedate" name="zqNewChangedate" th:field="*{zqNewChangedate}" class="time-input" type="text">
				</div>
			</div>
			<div class="form-group">	
				<label class="col-sm-3 control-label">信息来源：</label>
				<div class="col-sm-8">
					<input id="zqOriginal" name="zqOriginal" th:field="*{zqOriginal}" class="form-control" type="text">
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">一级类别：</label>
				<div class="col-sm-8">
					<select id="category1"  class="form-control m-b"  name="zqLb1"
							data-rel="chosen">
					</select>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">二级类别：</label>
				<div class="col-sm-8">
					<select id="category2"  class="form-control m-b"  name="zqLb2"
							data-rel="chosen">
					</select>
				</div>
			</div>

			<div class="form-group">
				<label class="col-sm-3 control-label">三级类别：</label>
				<div class="col-sm-8">
					<select id="category3"  class="form-control m-b"  name="zqLb3"
							data-rel="chosen">
					</select>
				</div>
			</div>

			<div class="form-group">
				<label class="col-sm-3 control-label">所在省：</label>
				<div class="col-sm-8">
					<select id="area1"  class="form-control m-b"  name="zqProvince"
							data-rel="chosen">
					</select>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">所在市：</label>
				<div class="col-sm-8">
					<select id="area2"  class="form-control m-b"  name="zqCity"
							data-rel="chosen">
					</select>
				</div>
			</div>

			<div class="form-group">
				<label class="col-sm-3 control-label">所在县：</label>
				<div class="col-sm-8">
					<select id="area3"  class="form-control m-b"  name="zqCountry"
							data-rel="chosen">
					</select>
				</div>
			</div>
			<div class="form-group">	
				<label class="col-sm-3 control-label">原名称：</label>
				<div class="col-sm-8">
					<input id="zqOldname" name="zqOldname" th:field="*{zqOldname}" class="form-control" type="text">
				</div>
			</div>
			<div class="form-group">	
				<label class="col-sm-3 control-label">新名称：</label>
				<div class="col-sm-8">
					<input id="zqNewname" name="zqNewname" th:field="*{zqNewname}" class="form-control" type="text">
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">详细内容：</label>
				<div class="col-sm-8">
					<input id="zqDetails" name="zqDetails" th:field="*{zqDetails}" type="hidden">
					<div id="editor" class="summernote"></div>
				</div>
			</div>
		</form>
    </div>
    <div th:include="include::footer"></div>
	<script th:src="@{/ajax/libs/summernote/summernote.min.js}"></script>
	<script th:src="@{/ajax/libs/summernote/summernote-zh-CN.js}"></script>
    <script type="text/javascript" th:inline="javascript">
		var prefix = ctx + "system/zhengqu";
		var prefix1 = ctx + "system/category"
		var prefix2 = ctx + "system/area"
		$(function() {

			var lb1 =   [[${zhengqu.zqLb1}]];
			var lb2 =   [[${zhengqu.zqLb2}]];
			var lb3 =   [[${zhengqu.zqLb3}]];

			var szx1 =   [[${zhengqu.zqProvince}]];
			var szx2 =   [[${zhengqu.zqCity}]];
			var szx3 =   [[${zhengqu.zqCountry}]];

			initCategrey(lb1,lb2,lb3);
			initArea(szx1,szx2,szx3);
			$('.summernote').summernote({
				height : '220px',
				lang : 'zh-CN',
				callbacks: {
					onImageUpload: function (files) {
						sendFile(files[0], this);
					}
				}
			});
			var content = $("#zqDetails").val();
			$('#editor').summernote('code', content);
		});

		// 上传文件
		function sendFile(file, obj) {
			var data = new FormData();
			data.append("file", file);
			$.ajax({
				type: "POST",
				url: ctx + "common/upload",
				data: data,
				cache: false,
				contentType: false,
				processData: false,
				dataType: 'json',
				success: function(result) {
					if (result.code == web_status.SUCCESS) {
						// $(obj).summernote('editor.insertImage', result.url, result.fileName);
                        $(obj).summernote('editor.insertImage', img_root + result.fileName, result.fileName);
					} else {
						$.modal.alertError(result.msg);
					}
				},
				error: function(error) {
					$.modal.alertWarning("图片上传失败。");
				}
			});
		}

		$("#form-zhengqu-edit").validate({
			rules:{
				xxxx:{
					required:true,
				},
			},
			focusCleanup: true
		});
		
		function submitHandler() {
	        if ($.validate.form()) {
				var sHTML = $('.summernote').summernote('code');
				$("#zqDetails").val(sHTML);
	            $.operate.save(prefix + "/edit", $('#form-zhengqu-edit').serialize());
	        }
	    }

		function initCategrey(zqLb1,zqLb2,zqLb3) {


			cate1(zqLb1);
			cate2(zqLb1,zqLb2);
			cate3(zqLb2,zqLb3);
			$("#category1").change(function () {
				cate2($("#category1").val());

			});
			$("#category2").change(function () {
				cate3($("#category2").val());
			});

		}
		function cate1(id) {
			$.get(
					prefix1 + "/list?parentId=100&deptId=4",
					function (data) {
						if (data) {
							var result = "";
							$.each(data, function (n, value) {

								if(value.deptId ==id )
									result += "<option selected= 'true' value='" + value.deptId + "'>" + value.deptName + "</option>";
								else
									result += "<option value='" + value.deptId + "'>" + value.deptName + "</option>";

							});
							$("#category1").html(result);
						}
					}, "json");
		}

		function cate2(id,deptId) {
			$.get(
					prefix1 + "/list?parentId=4",
					function (data) {
						if (data) {
							var result = "";
							$.each(data, function (n, value) {

								if(value.deptId ==deptId )
									result += "<option selected= 'true' value='" + value.deptId + "'>" + value.deptName + "</option>";
								else
									result += "<option value='" + value.deptId + "'>" + value.deptName + "</option>";

							});
							$("#category2").html(result);

						}
					}, "json");
		}
		function cate3(id,deptId) {
            if (!id) id = -100;
            $.get(
					prefix1 + "/list?parentId=" + id ,
					function (data) {
						if (data) {
							var result = '';
							$.each(data, function (n, value) {
								if(value.deptId ==deptId )
									result += "<option selected= 'true' value='" + value.deptId + "'>" + value.deptName + "</option>";
								else
									result += "<option value='" + value.deptId + "'>" + value.deptName + "</option>";
							});

							$("#category3").html(result);
						}
					}, "json");
		}

		function initArea(zqProvince,zqCity,zqCountry) {


			farea1(zqProvince);
			farea21(zqProvince,zqCity);
			farea3(zqCity,zqCountry);
			$("#area1").change(function () {
				farea2($("#area1").val());

			});
			$("#area2").change(function () {
				farea3($("#area2").val());
			});

		}
		function farea1(id) {
			$.get(
					prefix2 + "/list?parentId=1",
					function (data) {
						if (data) {
							var result = "";
							$.each(data, function (n, value) {

								if(value.deptId ==id )
									result += "<option selected= 'true' value='" + value.deptId + "'>" + value.deptName + "</option>";
								else
									result += "<option value='" + value.deptId + "'>" + value.deptName + "</option>";

							});
							$("#area1").html(result);
						}
					}, "json");
		}

		function farea2(id,deptId) {
			$.get(
					prefix2 + "/list?parentId="+ id,
					function (data) {
						if (data) {
							var result = "";
							$.each(data, function (n, value) {


								if(value.deptId ==deptId )
								{
									result += "<option selected= 'true' value='" + value.deptId + "'>" + value.deptName + "</option>";
								}
								else
								{
									result += "<option value='" + value.deptId + "'>" + value.deptName + "</option>";
									if(n==0)
									{
										farea3(value.deptId);
									}
								}
							});
							$("#area2").html(result);

						}
					}, "json");
		}

		function farea21(id,deptId) {
			$.get(
					prefix2 + "/list?parentId="+ id,
					function (data) {
						if (data) {
							var result = "";
							$.each(data, function (n, value) {


								if(value.deptId ==deptId )
								{
									result += "<option selected= 'true' value='" + value.deptId + "'>" + value.deptName + "</option>";
								}
								else
								{
									result += "<option value='" + value.deptId + "'>" + value.deptName + "</option>";
								}
							});
							$("#area2").html(result);

						}
					}, "json");
		}

		function farea3(id,deptId) {
			$.get(
					prefix2 + "/list?parentId=" + id ,
					function (data) {
						if (data) {
							var result = '';
							$.each(data, function (n, value) {
								if(value.deptId ==deptId )
									result += "<option selected= 'true' value='" + value.deptId + "'>" + value.deptName + "</option>";
								else
									result += "<option value='" + value.deptId + "'>" + value.deptName + "</option>";
							});

							$("#area3").html(result);
						}
					}, "json");
		}
	</script>
</body>
</html>
